package com.zf.actuator.endpoint;

import org.springframework.boot.actuate.endpoint.annotation.*;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义端点
 */
@Component
// 需要在 management.endpoints.web.exposure.include属性中配置暴露出 myCustomEndpoint 端点
@Endpoint(id = "myCustomEndpoint")
public class MyCustomEndpoint {

    /**
     * <pre>
     * {@code
     *    @ReadOperation 对应 @GetMapping，不支持请求体参数
     *    请求示例 RESTFUL 风格：GET http://ip:port/actuator/myCustomEndpoint/lisi/20
     *    路径参数：
     *    @param username
     *    @param age
     * }
     * </pre>
     */
    @ReadOperation
    public Map<String, Object> getEp(@Selector String username, @Selector Integer age) {
        Map<String, Object> res = new HashMap<>();
        res.put("type", HttpMethod.GET.toString());
        res.put("username", username);
        res.put("age", age);
        return res;
    }

    /**
     * <pre>
     * {@code
     * @WriteOperation 对应 @PostMapping
     * 请求示例 RESTFUL 风格：POST http://ip:port/actuator/myCustomEndpoint
     * 请求体参数；
     *  {
     *      "username": "zhangsan",
     *      "age": 20
     *  }
     *
     * @param username 参数都为必填项
     * @param age 参数都为必填项
     * }
     * </pre>
     */
    @WriteOperation
    public Map<String, Object> postEp(String username, Integer age) {
        Map<String, Object> res = new HashMap<>();
        res.put("type", HttpMethod.POST.toString());
        res.put("username", username);
        res.put("age", age);
        return res;
    }

    /**
     * <pre>
     * {@code
     * @DeleteOperation 对应 @DeleteMapping，不支持请求体参数
     * 请求示例 RESTFUL 风格：DELETE http://ip:port/actuator/myCustomEndpoint/id
     * 路径参数:
     * @param password
     * }
     * </pre>
     */
    @DeleteOperation
    public Map<String, Object> delEp(@Selector String id) {
        Map<String, Object> res = new HashMap<>();
        res.put("type", HttpMethod.DELETE.toString());
        res.put("id", id);
        return res;
    }

}
